package com.example.xinhuaxin.controller;

/**
 * @description:
 * @author: YancyZhang
 * @time: 2022/6/1 13:34
 */
import com.example.xinhuaxin.entity.User;
import com.example.xinhuaxin.service.impl.CardServiceImpl;
import com.example.xinhuaxin.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.util.Map;
import java.util.UUID;

@Controller//RestController返回实体对象，Controller返回页面
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    CardServiceImpl cardService;


    @RequestMapping("/aboutUs")
    public String aboutUs(){
        return "about_us";
    }
    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        return "login";
    }

    @RequestMapping("/register")
    public String register(HttpServletRequest request){
        return "register";
    }

    //用户注册
    @RequestMapping("/user_register")
    public String userRegister(User u, Model model){
        if(userService.userCheck(u.getUser_account())!= null){
            model.addAttribute("msg","账户已经被注册，请登录！");
            //注册成功 取登录
            return "login";
        }
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        u.setUser_id(str);
        userService.register(u);
        model.addAttribute("msg","注册成功，请登录！");
        //注册成功 取登录
        return "login";
    }


    //判断用户合法性
    @RequestMapping("/check")
    public String check(HttpServletRequest req, Model model){
        String account = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //检测登录
        User res = userService.login(account, pwd);
        if(res!=null){
            HttpSession session = req.getSession();
            session.setAttribute("card_system_user_id", res.getUser_id());
            model.addAttribute("msg","登录成功！");
            model.addAttribute("cardList",cardService.getCardList());
            return "index";
        }else{
            return "error";
        }
    }

//    登录成功，跳转到主页
    @RequestMapping("/success")
    public String succuessLogin(){
        return "success";
    }

    //个人主页 获取用户信息
    @RequestMapping("/userInfo")
    public String userInfo(HttpServletRequest req, Model model){
        HttpSession session = req.getSession();
        //根据用户id获取对应信息
        User u = userService.getUserInfo(session.getAttribute("card_system_user_id").toString());
        model.addAttribute("user_info", u);
        //获取绑定卡列表
        model.addAttribute("user_card_list",userService.getUserCardList(session.getAttribute("card_system_user_id").toString()));
        return "userInfo";
    }

    @RequestMapping("/question")
    public String toQuestion(){
        return "question";
    }

}